package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.constant.dto.BaseFxResponse;
import com.crm.constant.dto.ResultCode;
import com.crm.entity.CrmEmployee;
import com.crm.entity.CrmEmployeeRole;
import com.crm.entity.CrmRole;
import com.crm.mapper.CrmEmployeeMapper;
import com.crm.mapper.CrmRoleMapper;
import com.crm.model.bo.EmployeeBo;
import com.crm.model.bo.EmployeePageBo;
import com.crm.model.bo.LoginBo;
import com.crm.model.dto.BasePageResult;
import com.crm.model.vo.EmployeeVo;
import com.crm.model.vo.RoleVo;
import com.crm.service.ICrmEmployeeRoleService;
import com.crm.service.ICrmEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.service.ICrmRoleService;
import com.crm.utils.EncryptUtil;
import com.crm.utils.JwtUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * CRM-员工表 服务实现类
 * </p>
 *
 * @author CRM_SYSTEM
 * @since 2022-03-30
 */
@Service
public class CrmEmployeeServiceImpl extends ServiceImpl<CrmEmployeeMapper, CrmEmployee> implements ICrmEmployeeService {

    @Autowired
    private CrmRoleMapper roleMapper;
    @Autowired
    private ICrmEmployeeRoleService employeeRoleService;
    @Autowired
    private ICrmRoleService roleService;

    @Override
    public BaseFxResponse<?> login(LoginBo loginBo) {
        CrmEmployee employee = this.getOne(new LambdaQueryWrapper<CrmEmployee>()
                .eq(CrmEmployee::getPhone, loginBo.getPhone())
                .eq(CrmEmployee::getIsDeleted, false));
        if (Objects.isNull(employee)){
            return BaseFxResponse.fail(ResultCode.EXCEPTION_NOT_EMPLOYEE);
        }
        //原文进行加密
        String md5 = EncryptUtil.MD5(loginBo.getPassword());
        if (!md5.equals(employee.getPassword())){
            return BaseFxResponse.fail(ResultCode.EXCEPTION_PASSWORD_ERROR);
        }

        //校验成功生成Token
        String token = JwtUtils.token(employee.getPhone(),employee.getName());
        return BaseFxResponse.success(token);
    }

    @Override
    public EmployeeVo queryByPhone(String phone) {
        EmployeeVo employeeVo=null;
        CrmEmployee employee = this.getOne(new LambdaQueryWrapper<CrmEmployee>()
                .eq(CrmEmployee::getPhone, phone)
                .eq(CrmEmployee::getIsDeleted, false));
        if (Objects.nonNull(employee)){
            employeeVo=new EmployeeVo();
            BeanUtils.copyProperties(employee,employeeVo);
            List<RoleVo> roleVoList=roleMapper.queryByEmployeeId(employee.getId());
            employeeVo.setRoleList(roleVoList);
        }
        return employeeVo;
    }

    @Override
    public BasePageResult<CrmEmployee> queryPage(EmployeePageBo employeePageBo) {
        IPage<CrmEmployee> pageParam = new Page<>(employeePageBo.getPageNumber(), employeePageBo.getPageSize());
        LambdaQueryWrapper<CrmEmployee> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(employeePageBo.getName())){
            wrapper.like(CrmEmployee::getName,employeePageBo.getName());
        }
        if (StringUtils.isNotBlank(employeePageBo.getPhone())){
            wrapper.like(CrmEmployee::getPhone,employeePageBo.getPhone());
        }
        if (StringUtils.isNotBlank(employeePageBo.getIdCard())){
            wrapper.like(CrmEmployee::getIdCard,employeePageBo.getIdCard());
        }
        IPage<CrmEmployee> employeeIPage = this.page(pageParam, wrapper);
        return BasePageResult.newInstance(employeeIPage);
    }

    @Override
    public BaseFxResponse<Long> saveEmp(EmployeeBo employeeBo) {
        CrmEmployee employee = this.getOne(new LambdaQueryWrapper<CrmEmployee>()
                .eq(CrmEmployee::getPhone, employeeBo.getPhone())
                .eq(CrmEmployee::getIsDeleted, false));
        if (Objects.nonNull(employee)){
          return   BaseFxResponse.fail(ResultCode.EXCEPTION_EMPLOYEE_EXIST);
        }
        employee=new CrmEmployee();
        BeanUtils.copyProperties(employeeBo,employee);
        //原文进行加密
        String md5 = EncryptUtil.MD5(employeeBo.getPassword());
        employee.setPassword(md5);
        boolean save = this.save(employee);
        if (save){
            Long employeeId = employee.getId();
//            Long employeeId = this.getOne(new LambdaQueryWrapper<CrmEmployee>()
//                    .eq(CrmEmployee::getPhone, employeeBo.getPhone())
//                    .eq(CrmEmployee::getIsDeleted, false)).getId();
            CrmRole crmRole = roleService.getOne(new LambdaQueryWrapper<CrmRole>()
                    .eq(CrmRole::getRoleCode, 100001));
            CrmEmployeeRole employeeRole=new CrmEmployeeRole();
            employeeRole.setEmployeeId(employeeId);
            employeeRole.setRoleId(crmRole.getId());
            employeeRole.setStatus(1);
            employeeRoleService.save(employeeRole);
            return   BaseFxResponse.success(employeeId);
        }else {
            return  BaseFxResponse.fail(ResultCode.EXCEPTION);
        }
    }

    @Override
    public BaseFxResponse<Long> updateEmp(EmployeeBo employeeBo) {
        CrmEmployee employee = this.getOne(new LambdaQueryWrapper<CrmEmployee>()
                .eq(CrmEmployee::getPhone, employeeBo.getPhone())
                .eq(CrmEmployee::getIsDeleted, false)
        .ne(CrmEmployee::getId,employeeBo.getId()));
        if (Objects.nonNull(employee)){
            return   BaseFxResponse.fail(ResultCode.EXCEPTION_EMPLOYEE_EXIST);
        }
        employee=new CrmEmployee();
        BeanUtils.copyProperties(employeeBo,employee);
        this.updateById(employee);
        return BaseFxResponse.success(employeeBo.getId());
    }

    @Override
    public EmployeeVo queryById(Long employeeId) {
        CrmEmployee employee = this.getOne(new LambdaQueryWrapper<CrmEmployee>()
                .eq(CrmEmployee::getId, employeeId)
                .eq(CrmEmployee::getIsDeleted, false));
        return coverEmployee(employee);
    }

    public EmployeeVo coverEmployee( CrmEmployee employee){
        if (Objects.nonNull(employee)){
            EmployeeVo employeeVo=new EmployeeVo();
            BeanUtils.copyProperties(employee,employeeVo);
            return employeeVo;
        }
        return null;
    }
}
